package com.changgou.controller;

import com.changgou.goods.pojo.Sku;
import com.changgou.service.SkuService;
import com.github.pagehelper.PageInfo;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author : 残影
 * @Date: 2020/7/29 13:44
 */
@RestController
@CrossOrigin
@RequestMapping("/sku")
@Api(tags = "Sku")
public class SkuController {
    @Autowired
    private SkuService skuService;

    @ApiOperation("查询全部数据")
    @GetMapping
    public Result<List<Sku>> findAll(){
        List<Sku> list = skuService.findAll();
        return new Result<List<Sku>>(true, StatusCode.OK,"查询成功",list) ;
    }

    @ApiOperation("根据ID查询数据")
    @GetMapping("/{id}")
    public Result<Sku> findById(@PathVariable String id){
        Sku sku = skuService.findById(id);
        return new Result<Sku>(true,StatusCode.OK,"查询成功",sku);
    }

    @ApiOperation("新增数据")
    @PostMapping
    public Result add(@RequestBody Sku sku){
        skuService.add(sku);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    @ApiOperation("修改数据")
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "Sku对象",value = "传入JSON数据",required = false) Sku sku,@PathVariable String id){
        //设置主键值
        sku.setId(id);
        skuService.update(sku);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    @ApiOperation("根据ID删除数据")
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        skuService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    @ApiOperation("条件分页查询")
    @PostMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestBody(required = false) @ApiParam(name = "Sku对象",value = "传入JSON数据",required = false) Sku sku, @PathVariable  int page, @PathVariable  int size){
        PageInfo<Sku> pageInfo = skuService.findPage(sku, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    @ApiOperation("分页搜索实现")
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@PathVariable  int page, @PathVariable  int size){
        PageInfo<Sku> pageInfo = skuService.findPage(page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    @ApiOperation("多条件搜索品牌数据")
    @PostMapping(value = "/search" )
    public Result findList(@RequestBody(required = false) @ApiParam(name = "Sku对象",value = "传入JSON数据",required = false) Sku sku){
        List<Sku> list = skuService.findList(sku);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }
}
